package com.scores.controller;


import com.scores.pojo.Student;
import com.scores.service.StudentService;
import com.scores.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/studentLogin")
    public Results<Student> login(String userName, String password, String userCode, HttpSession session) {
        if (CodeController.isEquals(userCode, session)) {
            Student student = new Student(Integer.getInteger(userName), password);
            List<Student> result = studentService.getAllStudentsByPage(student, 0, 1);
            if (!result.isEmpty()) {
                Student loggedInStudent = result.get(0);
                session.setAttribute("loginUser", loggedInStudent);
                return new Results<>("0", "登录成功");
            } else {
                return new Results<>("2", "用户名或密码错误");
            }
        } else {
            return new Results<>("1", "验证码错误");
        }
    }

    @RequestMapping("/getStudents")
    public Results<List<Student>> getStudentsByPage() {
        List<Student> studentList;
        studentList = studentService.getAllStudents();
        int count = studentService.getAllStudentsCount(new Student());
        return new Results<>("0", "success", studentList, count);
    }

    @RequestMapping("/getAllStudent")
    public Results<List<Student>> getAllStudentsByPage(Student student,
                                                       @RequestParam("page") Integer page,
                                                       @RequestParam("limit") Integer pageSize) {
        List<Student> studentList;
        if (page == null || pageSize == null) {
            studentList = studentService.getAllStudentsByPage(student, 0, 10);
        } else {
            studentList = studentService.getAllStudentsByPage(student, (page - 1) * pageSize, pageSize);
        }
        int count = studentService.getAllStudentsCount(student);
        return new Results<>("0", "success", studentList, count);
    }

    @RequestMapping("/addStudent")
    public Results<String> addStudent(Student student) {
        int resultCode = studentService.addStudent(student);
        if (resultCode == 0) {
            return new Results<>("0", "添加成功", null, 0);
        } else if (resultCode == 1) {
            return new Results<>("1", "学号已存在", null, 0);
        } else {
            return new Results<>("2", "添加失败", null, 0);
        }
    }

    @RequestMapping("/updateStudent")
    public Results<String> updateStudent(Student student) {
        int resultCode = studentService.updateStudent(student);
        if (resultCode == 0) {
            return new Results<>("0", "更新成功", null, 0);
        } else {
            return new Results<>("1", "更新失败", null, 0);
        }
    }

    @RequestMapping("/deleteStudent")
    public Results<String> deleteStudent(Student student) {
        int count = studentService.deleteStudent(student);
        if (count == 0) {
            return new Results<>("0", "删除成功", null, 0);
        }else if(count == 2) {
            return new Results<>("2", "该同学还有成绩数据，不能删除", null, 0);
        } else {
            return new Results<>("1", "删除失败", null, 0);
        }
    }

    @RequestMapping("/updatePassword")
    public Results<String> updatePassword(String oldPassword, String newPassword, HttpSession session) {
        Student student = (Student) session.getAttribute("loginUser");
        String realPassword = student.getStudentPassword();
        if(!Objects.equals(realPassword, oldPassword)) {
            return new Results<>("1", "旧密码错误", null, 0);
        }
        if(Objects.equals(oldPassword, newPassword)) {
            return new Results<>("1", "新密码不能与旧密码相同", null, 0);
        }

        int resultCode = studentService.updatePassword(student.getStudentNo(), newPassword);
        if (resultCode == 0) {
            session.invalidate();
            return new Results<>("0", "密码更新成功，请重新登录", null, 0);
        } else {
            return new Results<>("2", "密码更新失败", null, 0);
        }

    }


}
